package com.atguigu.gmall.common.service;


import com.rabbitmq.client.ConfirmCallback;
import com.rabbitmq.client.ReturnCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
public class RabbitService{

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     *  发送消息
     * @param exchange 交换机
     * @param routingKey 路由键
     * @param message 消息
     */

    public boolean sendMessage(String exchange,String routingKey,Object message){
        rabbitTemplate.convertAndSend(exchange,routingKey,message);
        return true;// 成功
    }


    /**
     * 发送延迟消息
     * @param exchange 交换机
     * @param routingKey 路由键
     * @param message 消息
     * @param delayTime 单位：秒
     */
     public boolean sendDelayMessage(String exchange, String routingKey, Object message, int delayTime){
         rabbitTemplate.convertAndSend(exchange,routingKey,message,(msg)->{
             msg.getMessageProperties().setDelay(delayTime*1000);//延迟时间
             return msg;
         });
         return true;
     }
}
